package kr.ac.ml.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import ac.kr.ml.dao.ListDao;
import ac.kr.ml.vo.ContentVo;
import kr.ac.ml.mvc.Action;



public class WriteAction extends Action {

	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		
		System.out.println("WriteAction");
		
		String content = request.getParameter("content");
		String time;
		ContentVo vo = new ContentVo();
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat dataFormat = new SimpleDateFormat("yy.MM.dd HH:mm:ss");
		time = dataFormat.format(calendar.getTime());
		vo.setContent(content);		
		vo.setTime(time);
		vo.setBad(0);
		vo.setGood(0);
		vo.setReply_cont(0);

		ListDao dao = new ListDao();
		dao.insert(vo);
		
		
		String jsonstr = "";
		jsonstr +="{";
		jsonstr +="\"content\":[";
		jsonstr += "{";
		jsonstr += 		"\"con_id\":\"" +vo.getCon_id() + "\",";
		jsonstr += 		"\"content\":\"" + vo.getContent() + "\",";
		jsonstr +=		"\"time\":\"" + vo.getTime() + "\",";
		jsonstr += 		"\"good\":\"" + vo.getGood() + "\",";
		jsonstr += 		"\"bad\":\"" + vo.getBad() + "\",";
		jsonstr += 		"\"reply_cont\":\"" + vo.getReply_cont() + "\"";
		jsonstr += "}";
		
		jsonstr += "]";
		jsonstr += "}";
	
//		response.setContentType("text/html; charset=utf-8");
//		PrintWriter out = response.getWriter();
//		out.print(jsonstr);
		
		request.setAttribute("json", jsonstr);
		
				
	}

}
